// pages/record/record.js
const app = getApp()
const url = app.basicsUrl();

Page({
  data: {
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    tokenKey: '',
    token: '',
    openId: '',
    userImg: '',
    userName: '',
    userBlance: '',
    hongBaoPicks: [],
    hongBaoSends: [],
    robMoneyCount: '',
    robHongBaoNumCount: '',
    selfMoneyCount: '',
    selfHongBaoNumCount: '',
    nikeName: ''
  },

  /**
   * 提现跳转
   */
  bindWithdrawals: function () {
    var that = this;
    wx.navigateTo({
      url: '/pages/withdrawals/withdrawals'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /** 
   * 滑动切换tab 
   */
  bindChange: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },

  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  bindhongBao: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/games/games?hongBaoId=' + e.currentTarget.dataset.hongbaoid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    // that.init();
    that.wxLogin();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  wxLogin() {
    var that = this;
    wx.login({
      success: res => {
        var code = res.code;
        // 获取用户信息
        wx.getSetting({
          success: res => {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            wx.getUserInfo({
              success: res => {
                res.userInfo['code'] = code;
                var userInfo = res.userInfo;
                that.login(userInfo);
              },
              fail: res => {
                wx.showModal({
                  title: '提示',
                  content: '小程序需要获取用户信息权限，点击确认前往设置或退出程序',
                  showCancel: false,
                  mask: true,
                  success: function (res) {
                    if (res.confirm) {
                      that.checkLogin();
                    } else if (res.cancel) {
                    }
                  }
                })
              }
            })
          }
        })
      }
    })
  },
  login(userInfo) {
    var that = this;
    var params = {};
    params['jsCode'] = userInfo.code;
    params['nickname'] = userInfo.nickName;
    params['sex'] = userInfo.gender;
    params['city'] = userInfo.city;
    params['province'] = userInfo.province;
    params['headimgurl'] = userInfo.avatarUrl;
    wx.request({
      url: url + 'login',
      data: params,
      success: res => {
        var loginInfo = res.data.data;
        var nikeName = userInfo.nickName
        loginInfo['nikeName'] = nikeName;
        that.setData({ loginInfo: loginInfo })
        wx.setStorage({
          key: "loginInfo",
          data: loginInfo
        })
        that.init(loginInfo)
      }
    })
  },
  checkLogin() {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.openSetting({
            success: (res) => {
              if (res['authSetting']['scope.userInfo']) {
                // that.wxLogin();

              }
            }
          })
        }
      }
    })
  },
  init() {
    var that = this;
    wx.getStorage({
      key: 'loginInfo',
      success: function (res) {
        that.setData({
          tokenKey: res.data.id,
          token: res.data.token,
          openId: res.data.openId
        })
        var paramsself = {};
        paramsself['tokenKey'] = res.data.id,
        paramsself['token'] = res.data.token,
        paramsself['openId'] = res.data.openId,
        paramsself['size'] = 100,
        wx.request({
          url: url + "authorized/selfHome",
          data: paramsself,
          header: {
            'content-type': 'application/json'
          },
          success: res => {
            if (res.data.code == 200) {
              var hongBaoSends = res.data.data.hongBaoSends;
              for (var i = 0; i < hongBaoSends.length; i++) {
                hongBaoSends[i]['createTime'] = app.timeFormat(hongBaoSends[i]['createTime'] / 1000)
              }
              var hongBaoPicks = res.data.data.hongBaoPicks;
              for (var i = 0; i < hongBaoPicks.length; i++) {
                hongBaoPicks[i]['createTime'] = app.timeFormat(hongBaoPicks[i]['createTime'] / 1000)
              }
              that.setData({
                hongBaoPicks: hongBaoPicks,
                hongBaoSends: hongBaoSends,
                robMoneyCount: res.data.data.count.robMoneyCount,
                robHongBaoNumCount: res.data.data.count.robHongBaoNumCount,
                selfHongBaoNumCount: res.data.data.count.selfHongBaoNumCount,
                selfMoneyCount: res.data.data.count.selfMoneyCount,
                nikeName: decodeURI(res.data.data.user.name)
              })
            } else {
              wx.showLoading({
                title: res.data.msg,
              })
              setTimeout(function () {
                wx.hideLoading()
              }, 1000)
            }
          }
        })
      }
    })
  }
})